﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;

namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OUnsuitableProdAddV2 : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OUnsuitableProdList";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();

            }
        }

        protected void addmenu()
        {
            OUnsuitableProdInfo obj = Request.SerializableObject<OUnsuitableProdInfo>();
            obj.SupplerName = GetRequestValue<string>("dwz.opaV2Lookup.SupplerName", "");
            obj.AddType = 0;
            //2015-01-05
            if (!obj.FindPrdWorkShop && !obj.FindWarehouse)
            {
                AlertJson("发现场所 必需选一个.");
                return;
            }
            if (!obj.ERPFinished && !obj.ERPNotFinished)
            {
                AlertJson("ERP 必需选一个.");
            }
            if (!obj.NotionReturn && !obj.NotionOverhaul && !obj.NotionSpecial && !obj.NotionScrap)
            {
                AlertJson("处理意见 必需选一个.");
            }


            List<OUnsuitableProdImgInfo> listImg = new List<OUnsuitableProdImgInfo>();
            List<OUnsuitableProdDetailInfo> listDetail = new List<OUnsuitableProdDetailInfo>();

            char[] caSplit = new char[] { ',' };
            string[] titles = GetRequestValue<string>("title", "").Split(caSplit);
            string[] IMGAbsolutePaths = GetRequestValue<string>("IMGAbsolutePath", "").Split(caSplit);
            string[] IMGRelativelys = GetRequestValue<string>("IMGRelatively", "").Split(caSplit);

            //
            string[] DetailTypes = GetRequestValue<string>("DetailType", "").Split(caSplit);
            string[] DItemCodes = GetRequestValue<string>("DItemCode", "").Split(caSplit);
            string[] DItemNames = GetRequestValue<string>("DItemName", "").Split(caSplit);
            string[] DQuantitys = GetRequestValue<string>("DQuantity", "").Split(caSplit);
            string[] IsReturneds = GetRequestValue<string>("IsReturned", "").Split(caSplit);
            string[] IsDefectivePrds = GetRequestValue<string>("IsDefectivePrd", "").Split(caSplit);
            string[] IsScraps = GetRequestValue<string>("IsScrap", "").Split(caSplit);

            for (int index = 0; index < IMGRelativelys.Count(); index++)
            {
                if (IMGRelativelys[index] != "")
                {
                    OUnsuitableProdImgInfo itm = new OUnsuitableProdImgInfo();
                    itm.Title = titles[index];
                    itm.IMGAbsolutePath = IMGAbsolutePaths[index];
                    itm.IMGRelatively = IMGRelativelys[index];

                    listImg.Add(itm);
                }
            }

            for (int index = 0; index < DItemCodes.Count(); index++)
            {
                if (DItemCodes[index] != "")
                {

                    OUnsuitableProdDetailInfo itm = new OUnsuitableProdDetailInfo();
                    itm.DetailType = DetailTypes[index];
                    itm.DItemCode = DItemCodes[index];
                    itm.DItemName = DItemNames[index];
                    itm.DQuantity = Convert.ToDecimal(DQuantitys[index]);
                    itm.IsReturned = Convert.ToBoolean(IsReturneds[index] == "checked" || IsReturneds[index].ToLower() == "true" ? true : false);
                    itm.IsDefectivePrd = Convert.ToBoolean(IsDefectivePrds[index] == "checked" || IsDefectivePrds[index].ToLower() == "true" ? true : false);
                    itm.IsScrap = Convert.ToBoolean(IsScraps[index] == "checked" || IsScraps[index].ToLower() == "true" ? true : false);
                    //处理 不良品/报废原材料出库单 必选
                    if (itm.DetailType == "报废" && itm.IsDefectivePrd == false && itm.IsScrap == false)
                    {
                        AlertJson("不良品转库 或 报废处理 必需选一个.");
                    }
                    //
                    listDetail.Add(itm);
                }
            }


            int r = OUnsuitableProdController.Add(obj, listImg, listDetail);


            this.AddLog(Permission.新增, null, obj);

            if (r > 0)
                SuccessJson("添加成功", this.DWZRef);
            else
                ErrorJson("添加失败", this.DWZRef);
        }
    }
}